package com.jph.simple;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import adapter.GroupAdapter;
import container.GroupManager;
import model.Group;

public class GroupActivity extends Activity implements View.OnClickListener{

    public static final String TAG = "GroupActivity";

    private ImageView mIvTopIcon;
    private TextView mTvTopName;

    private ListView lvGroup;

    private List<Group> mGroupList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group);

        initTopView();

        lvGroup = (ListView) findViewById(R.id.lv_group);

        mGroupList.clear();
        mGroupList.addAll(GroupManager.getInstance().getGroupList());


        GroupAdapter groupAdapter = new GroupAdapter(this,
                R.layout.group_item, mGroupList);
        lvGroup.setAdapter(groupAdapter);                   //绑定数据和适配器
        lvGroup.setOnItemClickListener(new AdapterView.OnItemClickListener() {  //点击每一行的点击事件

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position,
                                    long id) {
                Group group= mGroupList.get(position);         //获取点击的那一行

                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString(PlayerChatActivity.ParamName, group.getName());
                bundle.putInt(PlayerChatActivity.ParamType, PlayerChatActivity.ChatTypeGroup);

                intent.setClass(GroupActivity.this, GroupChatActivity.class);
                intent.putExtras(bundle);

                startActivity(intent);
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_top_icon:
                backToMain();
                break;
        }
    }

    private void initTopView(){
        mIvTopIcon = (ImageView)findViewById(R.id.iv_top_icon);
        mTvTopName = (TextView)findViewById(R.id.tv_top_name);

        mIvTopIcon.setOnClickListener(this);

        mIvTopIcon.setImageResource(R.drawable.back_arrow);
        mTvTopName.setText(R.string.group_chat);
    }

    private void backToMain(){

        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra(MainActivity.PARAM, 1);

        startActivity(intent);
        finish();
        overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
    }
}
